
//从中序与后序遍历序列构造二叉树
   class TreeNode {
     int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
         this.left = left;
          this.right = right;
      }
  }

class Solution {
    public int postIndex;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        postIndex=postorder.length-1;
        return buildTreeChiler(inorder,postorder,0,inorder.length-1);
    }
    public TreeNode buildTreeChiler(int[] inorder, int[] postorder,int inbegin,int inend){
        if(inend<inbegin){
            return null;
        }
        TreeNode root=new TreeNode(postorder[postIndex]);

        int rootIndex=findVal(inorder,inend,inbegin,postorder[postIndex]);
        postIndex--;

        root.right=buildTreeChiler(inorder,postorder,rootIndex+1,inend);

        root.left=buildTreeChiler(inorder,postorder,inbegin,rootIndex-1);

        return root;
    }
    public int findVal(int[] inorder,int inend,int inbegin,int key){
        for(int i=inbegin;i<=inend;i++){
            if(inorder[i]==key){
                return i;
            }
        }
        return -1;
    }

}